package work.xiaohong.saolei.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import work.xiaohong.model.Result;
import work.xiaohong.model.StatusCode;
import work.xiaohong.saolei.pojo.Dianzhan;
import work.xiaohong.saolei.pojo.User;
import work.xiaohong.saolei.service.SaoleiDianzhanService;
import work.xiaohong.saolei.service.SaoleiUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "SaoleiDianzhan接口文档")
@RestController
@RequestMapping("/saoleiDianzhan")
public class SaoleiDianzhanController {

    @Autowired
    private SaoleiDianzhanService saoleiDianzhanService;

    @Autowired
    private SaoleiUserService saoleiUserService;

    /***
     * 分页条件搜索实现SaoleiDianzhan
     * @param dianzhan
     * @param page
     * @param size
     * @return
     */
    @Operation(summary = "条件分页查询SaoleiDianzhan",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) @Parameter(name = "SaoleiDianzhan对象",required = true) Dianzhan dianzhan, @PathVariable int page, @PathVariable  int size){
        //调用SaoleiDianzhanService实现分页条件查询SaoleiDianzhan
        Page pageInfo = saoleiDianzhanService.findPage(dianzhan, page, size);
        return new Result<Page>(true, StatusCode.OK.getCode(),"查询成功",pageInfo);
    }

    /***
     * 分页搜索实现SaoleiDianzhan
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @Operation(summary = "分页查询SaoleiDianzhan",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用SaoleiDianzhanService实现分页查询SaoleiDianzhan
        Page pageInfo = saoleiDianzhanService.findPage(page, size);
        return new Result<Page>(true,StatusCode.OK.getCode(),"查询成功",pageInfo);
    }

    /***
     * 多条件搜索SaoleiDianzhan数据
     * @param dianzhan
     * @return
     */
    @Operation(summary = "多条件查询SaoleiDianzhan",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PostMapping(value = "/search" )
    public Result<List<Dianzhan>> findList(@RequestBody(required = false) @Parameter(name = "SaoleiDianzhan对象",required = true) Dianzhan dianzhan){
        //调用SaoleiDianzhanService实现条件查询SaoleiDianzhan
        List<Dianzhan> list = saoleiDianzhanService.findList(dianzhan);
        return new Result<List<Dianzhan>>(true,StatusCode.OK.getCode(),"查询成功",list);
    }

    /***
     * 根据Id删除saoleiDianzhan数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID删除SaoleiDianzhan",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameter(in = ParameterIn.PATH, name = "id", description = "删除对象ID", required = true)
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Long id){
        //调用SaoleiDianzhanService实现根据ID删除
        saoleiDianzhanService.delete(id);
        return new Result(true,StatusCode.OK.getCode(),"删除成功");
    }

    /***
     * 根据ID修改SaoleiDianzhan数据
     * @param dianzhan
     * @return
     */
    @Operation(summary = "根据ID修改SaoleiDianzhan",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PutMapping
    public Result update(@RequestBody @Parameter(name = "SaoleiDianzhan对象",required = true) Dianzhan dianzhan){
        //调用SaoleiDianzhanService实现修改SaoleiDianzhan
        saoleiDianzhanService.update(dianzhan);
        return new Result(true,StatusCode.OK.getCode(),"修改成功");
    }

    /***
     * 点赞
     * @param dianzhan
     * @return
     */
    @Operation(summary = "点赞",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PostMapping
    @Transactional
    public Result add(@RequestBody  @Parameter(name = "SaoleiDianzhan对象",required = true) Dianzhan dianzhan){

        if (saoleiDianzhanService.findList(dianzhan) != null && saoleiDianzhanService.findList(dianzhan).size() > 0){
            // 已点赞
            return new Result(true,StatusCode.ERROR.getCode(),"已点赞");
        }

        // 用户点赞总数加一
        User user = saoleiUserService.findById(dianzhan.getReceiver());
        if (user != null){
            user.setDianzanTotal(user.getDianzanTotal()+1);
            // 更新用户数据
            saoleiUserService.updateById(user);
        }else {
            throw new RuntimeException("被点赞用户不存在！");
        }



        //调用SaoleiDianzhanService实现添加SaoleiDianzhan
        saoleiDianzhanService.add(dianzhan);
        return new Result(true,StatusCode.OK.getCode(),"点赞成功");
    }

    /***
     * 根据ID查询SaoleiDianzhan数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID查询SaoleiDianzhan",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameter(in = ParameterIn.PATH, name = "id", description = "查询对象ID", required = true)
    @GetMapping("/{id}")
    public Result<Dianzhan> findById(@PathVariable Long id){
        //调用SaoleiDianzhanService实现根据ID查询SaoleiDianzhan
        Dianzhan dianzhan = saoleiDianzhanService.findById(id);
        return new Result<Dianzhan>(true,StatusCode.OK.getCode(),"查询成功", dianzhan);
    }

    /***
     * 查询SaoleiDianzhan全部数据
     * @return
     */
    @Operation(summary = "查询所有SaoleiDianzhan",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @GetMapping
    public Result<List<Dianzhan>> findAll(){
        //调用SaoleiDianzhanService实现查询所有SaoleiDianzhan
        List<Dianzhan> list = saoleiDianzhanService.findAll();
        return new Result<List<Dianzhan>>(true, StatusCode.OK.getCode(),"查询成功",list) ;
    }
}
